package com.author.surface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.border.BevelBorder;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTMLDocument;

import com.author.document.style.StyleChangeNotify;

public final class RightIndentButton extends JButton implements ActionListener {
	/* X and Y coordinates of the visible lines */
	private int					x1, x2;
	private int					y1, y2;

	/* Display properties of the right indent button */
	private static final int	RI_BUTTON_WIDTH		= 32;
	private static final int	RI_BUTTON_HEIGHT	= 32;
	private static final int	RI_BUTTON_FONT_SIZE	= 24;

	/* Font used on the Right Indent button */
	private Font				riButtonFont		= new Font(fontName,
															Font.PLAIN,
															RI_BUTTON_FONT_SIZE);
	private static final String	fontName			= "Monospaced";

	/**
	 * The delta amount by which the left indent of a paragraph increases in
	 * response to a click on the right indent button
	 */
	public static final int		DELTA				= 20;

	public RightIndentButton() {
		// Set size of the RIght Indent button
		setSize(new Dimension(RI_BUTTON_WIDTH, RI_BUTTON_HEIGHT));
		setPreferredSize(getSize());
		setEnabled(true);

		// Set border of the Right Indent button
		setBorder(new BevelBorder(BevelBorder.LOWERED));
		setBorderPainted(false);

		// Set visible properties
		setFont(riButtonFont);
		setToolTipText("Increase Right Indent");
		setFocusable(false);
		setVisible(true);

		// Make left indent button responsive to user click
		addActionListener(this);
	}

	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		g2d.setColor(Color.BLACK);
		x1 = 25;
		y1 = 5;
		x2 = 15;
		y2 = 15;
		g2d.drawLine(x1, y1, x2, y2);
		x1 = 20;
		y1 = 5;
		x2 = 10;
		y2 = 15;
		g2d.drawLine(x1, y1, x2, y2);

		x1 = 10;
		y1 = 15;
		x2 = 20;
		y2 = 25;
		g2d.drawLine(x1, y1, x2, y2);
		x1 = 15;
		y1 = 15;
		x2 = 25;
		y2 = 25;
		g2d.drawLine(x1, y1, x2, y2);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae == null) {
			return;
		}

		if (!(ae.getSource() instanceof JButton)) {
			return;
		}

		/*
		 * Before the text is right indented, we check to see the left indent of
		 * the text. If left indent is null, or zero, right indent action is
		 * ignored. Otherwise, the left indent of the text is decreased by the
		 * DELTA amount specified. In case of a selection, the left indent of
		 * the paragraph element which has the minimum left indent value is
		 * considered.
		 */

		/* Get the current position of the dot and the mark in the text pane */
		int dot = TextPane.getDot();
		int mark = TextPane.getMark();

		Float newValue = null;
		if (dot == mark) { /* No selection */
			HTMLDocument htmlDoc = (HTMLDocument) TextPane
					.getTextPaneDocument();
			javax.swing.text.Element paraElem = htmlDoc
					.getParagraphElement(dot);

			Float currentValue = new Float(0);
			if (paraElem.getAttributes()
					.getAttribute(StyleConstants.LeftIndent) != null) {
				currentValue = (Float) paraElem.getAttributes().getAttribute(
						StyleConstants.LeftIndent);
			} else {
				return; /* Ignore action */
			}

			if (currentValue - DELTA < 0) {
				return; /* Ignore action */
			}

			newValue = new Float(currentValue - DELTA);
		} else {

			/*
			 * In case of a selection, consider the value for the paragraph
			 * element which has the lowest left indent value
			 */
			HTMLDocument htmlDoc = (HTMLDocument) TextPane
					.getTextPaneDocument();

			Float currentValue = new Float(0);
			javax.swing.text.Element paraElemAtDot = htmlDoc
					.getParagraphElement(dot);
			Float leftIndentValueAtDot = new Float(0);
			if (paraElemAtDot.getAttributes().getAttribute(
					StyleConstants.LeftIndent) != null) {
				leftIndentValueAtDot = (Float) paraElemAtDot.getAttributes()
						.getAttribute(StyleConstants.LeftIndent);
			}

			javax.swing.text.Element paraElemAtMark = htmlDoc
					.getParagraphElement(mark);
			Float leftIndentValueAtMark = new Float(0);
			if (paraElemAtMark.getAttributes().getAttribute(
					StyleConstants.LeftIndent) != null) {
				leftIndentValueAtMark = (Float) paraElemAtMark.getAttributes()
						.getAttribute(StyleConstants.LeftIndent);
			}

			currentValue = leftIndentValueAtDot <= leftIndentValueAtMark ? new Float(
					leftIndentValueAtDot) : new Float(leftIndentValueAtMark);

			if (currentValue - DELTA < 0) {
				return; /* Ignore action */
			}

			newValue = currentValue - DELTA;
		}

		/*
		 * Setting the right indent means just decrementing the left indent
		 * value (if it already exists), with the caution that the left indent
		 * value does not decrease below zero
		 */

		StyleChangeNotify styleChangeNotify = StyleChangeNotify
				.getStyleChangeNotify();
		styleChangeNotify.setLeftIndent("0", newValue.toString()); /*
																	 * Please
																	 * refer to
																	 * comments
																	 * in the
																	 * LeftIndentButton
																	 * class as
																	 * to why
																	 * the "0"
																	 * is sent
																	 */
	}
}
